package com.tianbing.adlottery.controller.hordeadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Site;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyServedStoreSectionController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyServedStoreSectionController.class.getName());

	private ISiteDao siteDao;

	private ISiteBusiness siteBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		String pageSectionName = ServletRequestUtils.getStringParameter(request, "section", "_TOP");

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site2Site sitePageSection = siteBusiness.findSitePageSection(ssid, pageSectionName);
		if (sitePageSection == null) {
			sitePageSection = new Site2Site();
		}
		fb.setSite2Site(sitePageSection);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);

		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?ssid=" + ssid);
		ControllerFormBean form = (ControllerFormBean) cmd;

		String pageSectionName = ServletRequestUtils.getStringParameter(request, "section", "_TOP");
		Site2Site s2s = form.getSite2Site();

		if (ssid > 0) {
			siteBusiness.saveSitePageSection(ssid, pageSectionName, s2s.getLongDesc());
		}

		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		String section = ServletRequestUtils.getStringParameter(request, "section", "_TOP");
		model.put("section", section);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site siteDetail = siteDao.findSiteBySysId(ssid);
		model.put("siteDetail", siteDetail);
		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyServedStoreSectionController.logger = logger;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}
}
